home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / TGA320.ZIP / 320ASM.ASM next >
Encoding:
Assembly Source File  |  1996-12-05  |  15.2 KB  |  471 lines

  1.  
  2. ; Tiny 320x200x256 gfx library, ASM function calls
  3.  
  4.     IDEAL
  5.     MODEL   small
  6.     CODESEG
  7.     LOCALS
  8.     P386
  9.  
  10.     PUBLIC putpixel,getpixel,mode320,modetext,vline,hline,vlinedata,hlinedata
  11.     PUBLIC clr320,drawbitmap,getbitmap,wait4vrt,bline,getpal,putpal,getpalcol
  12.     PUBLIC putpalcol,fillbox
  13.  
  14. ; void putpixel(int x, int y, char col);
  15. ; void mode320(void);
  16. ; void modetext(void);
  17. ; char getpixel(int x, int y);
  18. ; void bline(int sx, int sy, int ex, int ey, char col);
  19. ; void blinedata(int sx, int sy, int ex, int ey, char far *data);
  20. ; void hline(int y, char col);
  21. ; void hlinedata(int y, char far *data);
  22. ; void vline(int x, char col);
  23. ; void vlinedata(int x, char far *data);
  24. ; void clr320(char col);
  25. ; void drawbitmap(int x, int y, int xlen, int ylen, char far *data);
  26. ; void getbitmap(int x, int y, int xlen, int ylen, char far *data);
  27. ; void wait4vrt(void);
  28. ; void putpalcol(int col, char red, char green, char blue);
  29. ; void getpolcol(int col, char far *red, char far *green, char far *blue);
  30. ; void getpal(int startcol, char far *pal);
  31. ; void putpal(int startcol, char far *pal);
  32. ; int  getoffset(int x, int y);
  33. ; void circle(int x, int y, int rad, char col);
  34. ; void fillbox(int x, int y, int xlen, int ylen, char col);
  35.  
  36. ;---------------------------------------------------------------------------
  37. ; PutPixel      Writes a pixel
  38. ;---------------------------------------------------------------------------
  39. ; INPUT: AX - X
  40. ;        DX - Y
  41. ;        BL - Colour
  42. ; REGISTERS: DI
  43. PROC    putpixel
  44.         mov     di,ax
  45.         mov     ax,dx
  46.         shl     dx,8
  47.         shl     ax,6
  48.         add     ax,dx
  49.         add     di,ax
  50.         mov     al,bl
  51.         stosb                               ;write pixel
  52.         ret
  53. ENDP    putpixel
  54.  
  55. ;---------------------------------------------------------------------------
  56. ; Mode320       Sets mode 13h
  57. ;---------------------------------------------------------------------------
  58. ; REGISTERS: AX
  59. PROC    mode320                             ;get into mode 13h
  60.         mov     ax,13h
  61.         int     10h
  62.         ret
  63. ENDP    mode320
  64.  
  65. ;---------------------------------------------------------------------------
  66. ; ModeText      Sets standard text mode (3)
  67. ;---------------------------------------------------------------------------
  68. ; REGISTERS: AX
  69. PROC    modetext                            ;get into mode 3 (text)
  70.         mov     ax,3
  71.         int     10h
  72.         ret
  73. ENDP    modetext
  74.  
  75. ;---------------------------------------------------------------------------
  76. ; GetPixel      Gets a pixel from screen
  77. ;---------------------------------------------------------------------------
  78. ; INPUT:    AX - X
  79. ;           DX - Y
  80. ; OUTPUT:   AL - Color
  81. ; REGISTERS: SI
  82. PROC    getpixel
  83.         push    ds
  84.         mov     si,ax
  85.         mov     ax,es
  86.         mov     ds,ax                       ;set es to VGA seg
  87.         mov     ax,dx
  88.         shl     ax,6
  89.         shl     dx,8
  90.         add     ax,dx
  91.         add     si,ax
  92.         lodsb
  93.         pop     ds
  94.         ret
  95. ENDP    getpixel
  96. ;---------------------------------------------------------------------------
  97. ; HLine         Draws a horizontal line
  98. ;---------------------------------------------------------------------------
  99. ; INPUT:    AL - Colour
  100. ;           DX - Y
  101. ; REGISTERS: BX DI CX
  102. PROC    hline
  103.         mov     bx,dx
  104.         shl     bx,6
  105.         shl     dx,8
  106.         add     bx,dx
  107.         mov     di,bx
  108.         mov     cx,160
  109.         mov     ah,al
  110.         cld
  111.         rep     stosw                       ;fill line with col
  112.         ret
  113. ENDP    hline
  114. ;---------------------------------------------------------------------------
  115. ; VLine         Draws a vertical line
  116. ;---------------------------------------------------------------------------
  117. ; INPUT: AX - X
  118. ;        BL - Colour
  119. ; REGISTERS: CX DX DI
  120. PROC    vline
  121.         mov     di,ax                       ;set correct row
  122.         mov     al,bl
  123.         mov     cx,200
  124. @@Line:
  125.         stosb                               ;draw pixel
  126.         add     di,319                      ;just 319, since stosb incs di
  127.         dec     cx
  128.         ja      SHORT @@Line
  129.         ret
  130. ENDP    vline
  131. ;---------------------------------------------------------------------------
  132. ; HLineData     Draws a filled horizontal line
  133. ;---------------------------------------------------------------------------
  134. ; INPUT:    DX - Y
  135. ;           DS:SI - pointer to 200 bytes of data
  136. ; REGISTERS: CX DI
  137. PROC    hlinedata
  138.         mov     ax,dx
  139.         shl     ax,6
  140.         shl     dx,8
  141.         add     ax,dx
  142.         mov     di,ax
  143.         mov     cx,160
  144.         cld
  145.         rep     movsw                       ;mov from ds:si to es:di
  146.         ret
  147. ENDP    hlinedata
  148. ;---------------------------------------------------------------------------
  149. ; VLineData     Draws a filled vertical line
  150. ;---------------------------------------------------------------------------
  151. ; INPUT:    AX - X
  152. ;           DS:SI - pointer to 320 bytes of data
  153. ; REGISTERS: DI CX
  154. PROC    vlinedata
  155.         mov     di,ax                       ;es:di -> x,y
  156.         mov     cx,200
  157. @@Row:  movsb
  158.         add     di,319
  159.         dec     cx
  160.         jb      SHORT @@Row
  161.         ret
  162. ENDP    vlinedata
  163. ;---------------------------------------------------------------------------
  164. ; Clr320        Clears a 320 screen
  165. ;---------------------------------------------------------------------------
  166. ; INPUT:    AL - Color
  167. ; REGISTERS: EAX CX DI
  168. PROC    clr320
  169.         mov     ah,al
  170.         mov     cx,ax
  171.         rol     eax,10h
  172.         mov     ax,cx
  173.         mov     cx,16000
  174.         sub     di,di
  175.         cld
  176.         rep     stosd                       ;fill a000 with col
  177.         ret
  178. ENDP    clr320
  179. ;---------------------------------------------------------------------------
  180. ; DrawBitmap    Draws a section of data to screen
  181. ;---------------------------------------------------------------------------
  182. ; INPUT:    AX - X
  183. ;           DX - Y
  184. ;           CX - XLEN
  185. ;           BX - YLEN
  186. ;           DS:SI - pointer to data
  187. ; REGISTERS: DI GS
  188. PROC   drawbitmap
  189.         mov     gs,ax
  190.         mov     di,ax
  191.         mov     ax,dx
  192.         shl     ax,6
  193.         shl     dx,8
  194.         add     ax,dx
  195.         add     di,ax                       ;es:di -> x,y
  196.         mov     ax,gs
  197.         add     ax,320
  198.         sub     ax,cx
  199.         mov     dx,gs
  200.         sub     ax,dx                      ;find length between x+xlen and x+320
  201. @@Draw:
  202.         rep     movsb                       ;move line
  203.         add     di,ax                       ;next line
  204.         dec     bx
  205.         ja      SHORT @@Draw
  206.         ret
  207. ENDP    drawbitmap
  208. ;---------------------------------------------------------------------------
  209. ; GetBitmap     Reads a bitmap from screen
  210. ;---------------------------------------------------------------------------
  211. ; INPUT:    AX - X
  212. ;           DX - Y
  213. ;           CX - Xlen
  214. ;           BX - Ylen
  215. ;           DS:SI - pointer to save buffer
  216. ; REGISTERS: DI GS ES
  217. PROC    getbitmap
  218.         mov     gs,ax
  219.         push    ds
  220.         mov     ax,es
  221.         mov     ds,ax                       ;set ds to VGA seg
  222.         pop     es                          ;set es to buffer
  223.         mov     si,gs
  224.         mov     ax,dx
  225.         shl     ax,6
  226.         shl     dx,8
  227.         add     ax,dx
  228.         add     si,ax                       ;ds:si -> x,y
  229.         mov     ax,gs
  230.         add     ax,320
  231.         sub     ax,cx
  232.         mov     dx,gs
  233.         sub     ax,dx                      ;find length between x+xlen & x+320
  234.         mov     gs,cx
  235. @@Draw:
  236.         rep     movsb                       ;move line
  237.         add     si,ax
  238.         mov     cx,gs
  239.         dec     bx
  240.         ja      SHORT @@Draw
  241.         ret
  242. ENDP    getbitmap
  243. ;---------------------------------------------------------------------------
  244. ; Wait4Vrt      Wait for Vertical Retrace
  245. ;---------------------------------------------------------------------------
  246. ; REGISTERS: AX DX
  247. PROC    wait4vrt
  248.         mov     dx,3dah
  249. @@VRT:  in      al,dx                   ;Wait for VRT to start
  250.         test    al,8
  251.         jnz     @@VRT
  252. @@NoVRT:in      al,dx                   ;Wait for VRT to stop
  253.         test    al,8
  254.         jz      @@NoVRT
  255.         ret
  256. ENDP    wait4vrt
  257. ;---------------------------------------------------------------------------
  258. ; PutPalCol     Puts a color into the palette
  259. ;---------------------------------------------------------------------------
  260. ; INPUT:    AL  - Color Number
  261. ;           EBX - Color values (BL=Red, BH=Green, EBL=Blue)
  262. ; REGISTERS: DX
  263. PROC    putpalcol
  264.         mov     dx,3c8h
  265.         out     dx,al
  266.         inc     dx
  267.         mov     al,bl
  268.         out     dx,al
  269.         mov     al,bh
  270.         out     dx,al
  271.         ror     ebx,10h
  272.         mov     al,bl
  273.         out     dx,al
  274.         ret
  275. ENDP    putpalcol
  276. ;---------------------------------------------------------------------------
  277. ; GetPalCol     Get palette color
  278. ;---------------------------------------------------------------------------
  279. ; INPUT:    AL - Color number
  280. ; OUTPUT:   EBX - color (see PutPalCol)
  281. PROC    getpalcol
  282.         mov     dx,3c8h
  283.         out     dx,al
  284.         inc     dx
  285.         in      al,dx
  286.         mov     bl,al
  287.         in      al,dx
  288.         mov     bh,al
  289.         in      al,dx
  290.         rol     ebx,10h
  291.         mov     bl,al
  292.         rol     ebx,10h
  293.         ret
  294. ENDP    getpalcol
  295. ;---------------------------------------------------------------------------
  296. ; GetPal        Get the entire palette
  297. ;---------------------------------------------------------------------------
  298. ; INPUT:    AL - Start color
  299. ;           ES:DI - pointer to 768 bytes of save space
  300. ; REGISTERS: CX DX
  301. PROC    getpal
  302.         mov     dx,3c8h
  303.         out     dx,al
  304.         inc     dx
  305.         mov     cx,768
  306.         sub     cl,al
  307.         rep     insb
  308.         ret
  309. ENDP    getpal
  310. ;---------------------------------------------------------------------------
  311. ; PutPal        Puts the palette to VGA card
  312. ;---------------------------------------------------------------------------
  313. ; INPUT:    AL - start color
  314. ;           ES:DI - pointer to color palette
  315. ; OUTPUT:
  316. PROC    putpal
  317.         mov     dx,3c8h
  318.         out     dx,al
  319.         inc     dx
  320.         mov     cx,768
  321.         sub     cl,al
  322.         rep     outsb
  323.         ret
  324. ENDP    putpal
  325.  
  326. ;---------------------------------------------------------------------------
  327. ; BLine         Draws a Bresenham's Line
  328. ;---------------------------------------------------------------------------
  329. ; INPUT     AX - Start X
  330. ;           DX - Start Y
  331. ;           CX - End X
  332. ;           BX - End Y
  333. ;           DI - Color
  334. ; REGISTERS:
  335. PROC    bline
  336.         LOCAL   deltax:WORD, deltay:WORD, col:WORD,sx:WORD,sy:WORD
  337.         push    bp si
  338.         mov     bp,sp                       ;Setup Local Stack Frame
  339.         sub     bp,16                       ;Add space for LOCALS (actually 10)
  340.         mov     [sx],ax
  341.         mov     [sy],dx
  342.         mov     [col],di
  343.         sub     cx,ax                       ;d(elta)x = ex-sx
  344.         sub     bx,dx                       ;cx(deltay) =ey-sy
  345.  
  346.         mov     ax,bx
  347.  
  348.         cmp     cx,0
  349.         je      SHORT @@checky              ;deltax is 0
  350.         mov     bx,OFFSET cs:@@Xinc
  351. ;------------TESTING CX (end X)
  352.         bt      cx,0fh
  353.         jc      SHORT @@decx                ;deltax is negative
  354. @@incx:
  355.         ;inc     bl                          ;dx is horizontal to the right
  356.         mov     [WORD cs:bx],046FFh
  357.         mov     [BYTE cs:bx+02],0F8h          ;set incx to inc [bp-08]
  358.         jnc     SHORT @@checky
  359. @@decx:
  360.         ;dec     bl
  361.         mov     [WORD cs:bx],04EFFh
  362.         mov     [BYTE cs:bx+02],0F8h          ;set incx to dec [bp-08]
  363. @@checky:
  364.  
  365. ;-------------TESTING AX (end Y)
  366.         cmp     ax,0
  367.         je      SHORT @@InvX
  368.         mov     bx,OFFSET @@Yinc
  369.         bt      ax,0fh
  370.         jc      SHORT @@decy
  371. @@incy:
  372.         ;inc     bh
  373.         mov     [WORD cs:bx],046FFh
  374.         mov     [BYTE cs:bx+02],0F6h          ;set incy to inc [bp-0A]
  375.         jnc     SHORT @@InvX
  376. @@decy:
  377.         ;dec     bh
  378.         mov     [WORD cs:bx],04EFFh
  379.         mov     [BYTE cs:bx+02],0F6h          ;set incy to dec [bp-0A]
  380. @@InvY:
  381.         neg     ax                           ;inverse sign
  382. @@InvX:                                     ;inverse sign
  383.         cmp     cx,0
  384.         jns     SHORT @@CheckDist
  385.         neg     cx
  386. @@CheckDist:
  387.         mov     si,ax                       ;set distance to deltaY
  388.         cmp     cx,ax
  389.         jbe     SHORT @@disty               ;
  390.         mov     si,cx                       ;no, correct to deltaX
  391. @@disty:
  392.         mov     [deltax],cx
  393.         mov     [deltay],ax
  394.         mov     cx,si                       ;loopcount
  395.         xor     bx,bx
  396.         xor     dx,dx
  397. ;──────────────DrawLoop─────────────────────────────────────────────────
  398. @@DrawLoop:
  399.         add     bx,[deltax]                 ;xerr+=deltax
  400.         add     dx,[deltay]                 ;yerr+=deltay
  401.         cmp     bx,si                       ;if(xerr > distance)
  402.         jbe     SHORT @@ContinueX
  403.         sub     bx,si                       ;xerr-=distance
  404. @@Xinc: nop
  405.         nop
  406.         nop                                 ;here goes the inc/dec [bp+04]
  407. @@ContinueX:
  408. @@PastXSave:
  409.         cmp     dx,si                       ;if(yerr > distance)
  410.         jbe     SHORT @@ContinueY
  411.         sub     dx,si                       ;yerr-=distance
  412. @@Yinc: nop
  413.         nop
  414.         nop                                 ;here goes the inc/dec [bp+06]
  415. @@ContinueY:
  416.         mov     ax,[sy]
  417.         rol     edx,10h
  418.         mov     dx,ax
  419.         shl     ax,6
  420.         shl     dx,8
  421.         add     ax,dx
  422.         add     ax,[sx]
  423.         mov     di,ax                       ;es:di -> pixel
  424.         mov     ax,[col]                    ;put pixel in al
  425.         stosb                               ;write pixel
  426.         ror     edx,10h
  427.         dec     cx
  428.         cmp     cx,0
  429.         ja      SHORT @@DrawLoop
  430. @@GoHome:
  431.         pop     si bp
  432.         ret
  433. ENDP    bline
  434.  
  435. ;---------------------------------------------------------------------------
  436. ; FillBox       Draws a filled box to screen
  437. ;---------------------------------------------------------------------------
  438. ; INPUT:    AX - left
  439. ;           BX - top
  440. ;           CX - right
  441. ;           DX - bottom
  442. ;           DI - color
  443. ; REGISTERS: gs
  444. PROC    fillbox
  445.         push    di
  446.         mov     gs,ax
  447.         mov     di,ax                           ;StartX
  448.         mov     ax,bx
  449.         shl     ax,8
  450.         shl     bx,6
  451.         add     ax,bx
  452.         add     di,ax                           ;+StartY
  453.         shr     bx,6                            ;Restore reg
  454.         sub     dx,bx                           ;# line
  455.         mov     bx,gs
  456.         add     bx,320
  457.         sub     bx,cx                           ;len between lines
  458.         mov     ax,gs
  459.         sub     cx,ax                           ;Line length
  460.         mov     gs,cx
  461.         pop     ax                              ;get color
  462. @@HLoop:
  463.         rep     stosb
  464.         add     di,bx
  465.         mov     cx,gs                           ;Line length
  466.         dec     dx
  467.         ja      @@HLoop
  468.         ret
  469. ENDP    fillbox
  470.         END
  471.